<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-02-28
 * Time: 14:31
 */

class TopicRecommendModel extends MY_Model{
    public $table = 't_topic_recommend';
    public function __construct(){
        parent::__construct();
    }

    /**
     * 获取每一个话题的详情
     * name: get_details
     * @param array $data
     * @return array|bool
     * @Author:taiyang
     */
    public function get_details($data=array()){
        if(empty($data)){
            return false;
        }
        $res=$data;
        //获取话题详情
        $topic=$this->TopicModel->query_one(array('id'=>$data['topic_id'],'level'=>2));
        if(empty($topic)){return false;}
        $res['title']=$topic['title'];
        $res['post_num']=$topic['post_num'];
        $res['reply_num']=$topic['reply_num'];
        $res['play_num']=$topic['play_num'];
        $res['follow_user_num']=$topic['follow_user_num'];
        $res['rule']=json_decode($data['rule'],true);
        $res['specify']=json_decode($data['specify_id'],true);
        return $res;
    }


    public function get_where($id){
        $topic_res=$this->TopicRecommendModel->query_one(array('id'=>$id));
        $topic=$this->TopicRecommendModel->get_details($topic_res);
        $where=array(
            'topic'=>$topic_res['topic_id'],
            'add_time_s'=>date("Y-m-d",strtotime("-60day")),
            'add_time_e'=>date("Y-m-d",time()),
        );
        $order=2;
        if($topic['rule']){
            if($topic['rule']['is_hot']){
                $where['is_hot']=$topic['rule']['is_hot'];
            }
            if($topic['rule']['is_img']){
                $where['is_pic']=$topic['rule']['is_img'];
            }
            if($topic['rule']['post_time']<0){
                $s_time=date("Y-m-d",strtotime("-".$topic['rule']['set_post_time']."day"));
            }elseif($topic['rule']['post_time']>0){
                $s_time=date("Y-m-d",strtotime("-".$topic['rule']['post_time']."day"));
            }else{
                $s_time=date("Y-m-d",strtotime("-60day"));
            }
            $where['add_time_s']=$s_time;
            $where['add_time_e']=date("Y-m-d",time());
            if(!empty($topic['rule']['order'])){
                $order=$topic['rule']['order'];
            }
        }
        //不能重复提取的内容
        $not_id='';
        if($topic['remove_id']){
            $not_id.=$topic['remove_id'].',';
        }
        if($topic['specify']){
            foreach ($topic['specify'] as $k => $v){
                $id_in[$v['sort']]=$v['id'];
            }
            $not_id.=implode(',',$id_in);
        }
        $not_id_arr=explode(',',$not_id);
        $arr=array_filter(array_flip(array_flip($not_id_arr)));//去重，去空
        $where['not_in']=implode(',',$arr);
        return array('where'=>$where,'order'=>$order);
    }
}